﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xant.FlowEngine.Interface;

namespace Xant.FlowEngine.Core
{
    public abstract class AbstractWorkitem : AbstractFlowObject
    {
        #region 变量声明
        public delegate void BeforeActionHandler(AbstractWorkitem workitem, WorkAction action, bool cancel);
        public delegate void AfterActionHandler(AbstractWorkitem workitem);
        public BeforeActionHandler BeforeAction;
        public AfterActionHandler AfterAction;

        #endregion

        #region 属性

        public Guid ActivityId
        {
            get
            {
                return OwnerActivity == null ? Guid.Empty : OwnerActivity.Id;
            }
        }

        /// <summary>
        /// 所属活动
        /// </summary>
        public ManualActivity OwnerActivity { get; internal set; }

        /// <summary>
        /// 只供传阅
        /// </summary>
        public bool Readonly { get; internal set; }

        /// <summary>
        /// 参与者
        /// </summary>
        public IStaff Participant { get; internal set; }

        /// <summary>
        /// 审阅动作
        /// </summary>
        public WorkAction Action { get; internal set; }

        /// <summary>
        /// 操作时间
        /// </summary>
        public DateTime? OperateTime { get; internal set; }

        /// <summary>
        /// 意见
        /// </summary>
        public String Opinion { get; internal set; }

        public DateTime? EntryTime { get; internal set; }

        public DateTime? LeaveTime { get; internal set; }

        #endregion

        #region 业务方法
        /// <summary>
        /// 已阅
        /// </summary>
        public virtual void Read(DateTime operateTime, String opinion)
        {
            DoAction(WorkAction.Read, operateTime, opinion);
        }

        /// <summary>
        /// 通过
        /// </summary>
        public virtual void Pass(DateTime operateTime, String opinion)
        {
            DoAction(WorkAction.Pass, operateTime, opinion);
        }

        /// <summary>
        /// 退回
        /// </summary>
        public virtual void Retreat(DateTime operateTime, String opinion)
        {
            DoAction(WorkAction.Retreat, operateTime, opinion);
        }

        /// <summary>
        /// 终止
        /// </summary>
        public virtual void Terminate(DateTime operateTime, String opinion)
        {
            DoAction(WorkAction.Terminate, operateTime, opinion);
        }

        /// <summary>
        /// 用户操作
        /// </summary>
        /// <param name="action">动作类型</param>
        /// <param name="pperateTime">操作时间</param>
        /// <param name="opinion">意见</param>
        public virtual void DoAction(WorkAction action, DateTime operateTime, String opinion)
        {
            bool cancel = false;
            if (BeforeAction != null)
            {
                BeforeAction(this, action, cancel);
            }
            if (cancel)
            {
                return;
            }
            this.Action = action;
            this.OperateTime = operateTime;
            this.Opinion = opinion;
            if (AfterAction != null)
            {
                AfterAction(this);
            }
        }

        #endregion

    }
}
